package com.bong.panpan.bongcharttools.custom;

/**
 * Created by panpan on 2018/1/7.
 */

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

/**
 * 用于记录温度，生成csv文件
 */
public class TempRecord {
    HashMap<String, Boolean> macFiles = new HashMap<>();
    SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    public void record(String mac, Date date, float temp, float acc) {
        String timeString = dataFormat.format(date);
        String insertString = timeString + "," + temp + "," + acc;
        if (macFiles.get(mac)) {

        } else {
            try {
                File file = new File(mac + ".csv");
                if (file.exists()) {
                    macFiles.put(mac, true);
                } else {
                    if (file.createNewFile()) {

                    }
                }
            } catch (Exception e) {

            }
        }
    }

    /**
     * 追加文件：使用FileWriter
     *
     * @param fileName
     * @param content
     */
    public boolean appendText(String fileName, String... content) {
        FileWriter writer = null;
        try {
            // 打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
            writer = new FileWriter(fileName, true);
            for (int i = 0; i < content.length; i++) {
                writer.append(content[i] + "\n");
            }
        } catch (IOException e) {
            return false;
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                return false;
            }
        }
        return true;
    }
}
